<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>boost/python/type_id.hpp</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../utility_and_infrastructure.html" title="Chapter 7. Utility and Infrastructure">
<link rel="prev" href="boost_python_handle_hpp.html" title="boost/python/handle.hpp">
<link rel="next" href="boost_python_ssize_t_hpp.html" title="boost/python/ssize_t.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_handle_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../utility_and_infrastructure.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_ssize_t_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="utility_and_infrastructure.boost_python_type_id_hpp"></a><a class="link" href="boost_python_type_id_hpp.html" title="boost/python/type_id.hpp">boost/python/type_id.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.class_template_type_info">Class
        template <code class="computeroutput"><span class="identifier">type_info</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.functions">Functions</a></span></dt>
<dt><span class="section"><a href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="utility_and_infrastructure.boost_python_type_id_hpp.introduction"></a><a class="link" href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
          &lt;boost/python/type_id.hpp&gt; provides types and functions for runtime
          type identification like those of of <code class="computeroutput"><span class="special">&lt;</span><span class="identifier">typeinfo</span><span class="special">&gt;</span></code>.
          It exists mostly to work around certain compiler bugs and platform-dependent
          interactions with shared libraries.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="utility_and_infrastructure.boost_python_type_id_hpp.class_template_type_info"></a><a class="link" href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.class_template_type_info" title="Class template type_info">Class
        template <code class="computeroutput"><span class="identifier">type_info</span></code></a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.class_template_type_info.class_template_type_info_constru">Class
          template <code class="computeroutput"><span class="identifier">type_info</span></code> constructor</a></span></dt>
<dt><span class="section"><a href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.class_template_type_info.class_template_type_info_compari">Class
          template <code class="computeroutput"><span class="identifier">type_info</span></code> comparison</a></span></dt>
<dt><span class="section"><a href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.class_template_type_info.class_template_type_info_observe">Class
          template <code class="computeroutput"><span class="identifier">type_info</span></code> observers</a></span></dt>
</dl></div>
<p>
          <code class="computeroutput"><span class="identifier">type_info</span></code> instances identify
          a type. As <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">type_info</span></code> is specified to (but unlike
          its implementation in some compilers), <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">type_info</span></code>
          never represents top-level references or cv-qualification (see section
          5.2.8 in the C++ standard). Unlike <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">type_info</span></code>,
          <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">type_info</span></code> instances are copyable, and
          comparisons always work reliably across shared library boundaries.
        </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
  <span class="keyword">class</span> <span class="identifier">type_info</span> <span class="special">:</span> <span class="identifier">totally_ordered</span><span class="special">&lt;</span><span class="identifier">type_info</span><span class="special">&gt;</span>
  <span class="special">{</span>
  <span class="keyword">public</span><span class="special">:</span>
    <span class="comment">// constructor</span>
    <span class="identifier">type_info</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">type_info</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="special">=</span> <span class="keyword">typeid</span><span class="special">(</span><span class="keyword">void</span><span class="special">));</span>

    <span class="comment">// comparisons</span>
    <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span><span class="identifier">type_info</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
    <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span><span class="identifier">type_info</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>

    <span class="comment">// observers</span>
    <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
  <span class="special">};</span>
<span class="special">}}</span>
</pre>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="utility_and_infrastructure.boost_python_type_id_hpp.class_template_type_info.class_template_type_info_constru"></a><a class="link" href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.class_template_type_info.class_template_type_info_constru" title="Class template type_info constructor">Class
          template <code class="computeroutput"><span class="identifier">type_info</span></code> constructor</a>
</h4></div></div></div>
<pre class="programlisting"><span class="identifier">type_info</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">type_info</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="special">=</span> <span class="keyword">typeid</span><span class="special">(</span><span class="keyword">void</span><span class="special">));</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Effects</span></dt>
<dd><p>
                  constructs a <code class="computeroutput"><span class="identifier">type_info</span></code>
                  object which identifies the same type as its argument.
                </p></dd>
<dt><span class="term">Rationale</span></dt>
<dd><p>
                  Since it is occasionally necessary to make an array of <code class="computeroutput"><span class="identifier">type_info</span></code> objects a benign default
                  argument is supplied. Note: this constructor does not correct for
                  non-conformance of compiler <code class="computeroutput"><span class="keyword">typeid</span><span class="special">()</span></code> implementations. See <code class="computeroutput"><span class="identifier">type_id</span></code>, below.
                </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="utility_and_infrastructure.boost_python_type_id_hpp.class_template_type_info.class_template_type_info_compari"></a><a class="link" href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.class_template_type_info.class_template_type_info_compari" title="Class template type_info comparison">Class
          template <code class="computeroutput"><span class="identifier">type_info</span></code> comparison</a>
</h4></div></div></div>
<pre class="programlisting"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span><span class="identifier">type_info</span> <span class="keyword">const</span> <span class="special">&amp;</span><span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Effects</span></dt>
<dd><p>
                  yields a total order over <code class="computeroutput"><span class="identifier">type_info</span></code>
                  objects.
                </p></dd>
</dl>
</div>
<pre class="programlisting"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span><span class="identifier">type_info</span> <span class="keyword">const</span> <span class="special">&amp;</span><span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="keyword">true</span></code> iff the two values
                  describe the same type.
                </p></dd>
<dt><span class="term">Note</span></dt>
<dd><p>
                  The use of <code class="computeroutput"><span class="identifier">totally_ordered</span><span class="special">&lt;</span><span class="identifier">type_info</span><span class="special">&gt;</span></code> as a private base class supplies
                  operators <code class="computeroutput"><span class="special">&lt;=</span></code>,
                  <code class="computeroutput"><span class="special">&gt;=</span></code>, <code class="computeroutput"><span class="special">&gt;</span></code>, and <code class="computeroutput"><span class="special">!=</span></code>
                </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="utility_and_infrastructure.boost_python_type_id_hpp.class_template_type_info.class_template_type_info_observe"></a><a class="link" href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.class_template_type_info.class_template_type_info_observe" title="Class template type_info observers">Class
          template <code class="computeroutput"><span class="identifier">type_info</span></code> observers</a>
</h4></div></div></div>
<pre class="programlisting"><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                  The result of calling <code class="computeroutput"><span class="identifier">name</span><span class="special">()</span></code> on the argument used to construct
                  the object.
                </p></dd>
</dl>
</div>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="utility_and_infrastructure.boost_python_type_id_hpp.functions"></a><a class="link" href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.functions" title="Functions">Functions</a>
</h3></div></div></div>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">&lt;&lt;(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span><span class="identifier">s</span><span class="special">,</span> <span class="identifier">type_info</span> <span class="keyword">const</span><span class="special">&amp;</span><span class="identifier">x</span><span class="special">);</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Effects</span></dt>
<dd><p>
                Writes a description of the type described by to <code class="computeroutput"><span class="identifier">x</span></code>
                into s.
              </p></dd>
<dt><span class="term">Rationale</span></dt>
<dd><p>
                Not every C++ implementation provides a truly human-readable <code class="computeroutput"><span class="identifier">type_info</span><span class="special">::</span><span class="identifier">name</span><span class="special">()</span></code>
                string, but for some we may be able to decode the string and produce
                a reasonable representation.
              </p></dd>
<dt><span class="term">Note</span></dt>
<dd><p>
                On some non-conforming C++ implementations, the code is not actually
                as simple as described above; the semantics are adjusted to work
                as-if the C++ implementation were conforming.
              </p></dd>
</dl>
</div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">type_info</span> <span class="identifier">type_id</span><span class="special">()</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                <code class="computeroutput"><span class="identifier">type_info</span><span class="special">(</span><span class="keyword">typeid</span><span class="special">(</span><span class="identifier">T</span><span class="special">))</span></code>
              </p></dd>
<dt><span class="term">Note</span></dt>
<dd><p>
                On some non-conforming C++ implementations, the code is not actually
                as simple as described above; the semantics are adjusted to work
                as-if the C++ implementation were conforming.
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="utility_and_infrastructure.boost_python_type_id_hpp.example"></a><a class="link" href="boost_python_type_id_hpp.html#utility_and_infrastructure.boost_python_type_id_hpp.example" title="Example">Example</a>
</h3></div></div></div>
<p>
          The following example, though silly, illustrates how the type_id facility
          might be used
        </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">type_id</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="comment">// Returns true iff the user passes an int argument</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">bool</span> <span class="identifier">is_int</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">x</span><span class="special">)</span>
<span class="special">{</span>
   <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">;</span>
   <span class="keyword">return</span> <span class="identifier">type_id</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;()</span> <span class="special">==</span> <span class="identifier">type_id</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;();</span>
<span class="special">}</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_handle_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../utility_and_infrastructure.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_ssize_t_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
